<?php

class BranchesController extends AppController
{
    # Controller Name to be used
    var $name = 'Branches';

    # Models to be used
    var $uses = array(
        'Branch'
    );

    # set the layout to use
    var $layout = 'branch_layout';

    function  beforeFilter(){
        parent::beforeFilter();
    }

    function index($type = 'get'){

        if ($this->request->is('ajax')) {
            $this->autoRender = false;
            $this->autoLayout = false;
            switch ($type) {
                case 'get' :
                    /**  Get posted data */
                    $page = isset($_POST['page']) ? $_POST['page'] : 1;
                    /** The current page */
                    $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'id';
                    /** Sort column */
                    $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';
                    /** Sort order */
                    $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : '';
                    /** Search column */
                    $search_query = isset($_POST['query']) ? $_POST['query'] : '';
                    /** Search string */
                    $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
                    $limit = $rp;
                    $start = ($page - 1) * $rp;

                    $condition_array = array(
                        'Branch.deleted' => 'n'
                    );

                    if (!empty($search_query)) {
                        if ($qtype == 'name') {
                            $condition_array = array(
                                'Branch.name LIKE ' => $search_query.'%',
                                'Branch.deleted' => 'n'
                            );
                        }
                    }

                    $data_table = $this->Branch->find('all', array('conditions' => $condition_array, 'order' => "Branch.$sortname $sortorder", 'limit' => $start . ',' . $limit, 'recursive' => -1));
                    $data_table_count = $this->Branch->find('count', array('conditions' => $condition_array, 'recursive' => -1));

                    $total_records = $data_table_count;

                    if ($data_table) {
                        $return_arr = array();
                        foreach ($data_table as $obj) {
                            $return_arr[] = array(
                                'id' => $obj['Branch']['id'],
                                'cell' => array(
                                    $obj['Branch']['id'],
                                    $obj['Branch']['name']
                                ),
                                'property'=>array(
                                    'edit_row'=> $obj['Branch']['edit_row']
                                )
                            );
                        }
                        return json_encode(array('success' => true, 'total' => $total_records, 'page' => $page, 'rows' => $return_arr));
                    }
                    else {
                        return json_encode(array('success' => false, 'total' => $total_records, 'page' => $page, 'rows' => array()));
                    }
                    break;

                case 'save' :
                    $data = array('Branch' => $_POST);
                    $pass = '';
                    if ($_POST['id'] > 0) {
                        $data['Branch']['modified_by'] = $this->authUser['id'];
                    } else {
                        $data['Branch']['created_by'] = $this->authUser['id'];
                    }

                    if ($this->Branch->save($data['Branch'])) {
                        if($_POST['id'] > 0){
                            return json_encode(array('code' => 0, 'msg' => 'Data Saved'));
                        }
                        else{ //If new pass back extra data if needed.
                            return json_encode(array('code' => 0, 'msg' => 'Data Saved!', 'id'=>$this->Branch->id));
                        }
                    } else {
                        echo json_encode(array('code' => 1, 'msg' => 'Some errors occurred.'));
                    }
                    break;

                case 'delete':
                    $ids = $_POST['ids'];
                    $modObj = ClassRegistry::init('Branch');
                    $result = $modObj->updateAll(
                        array('Branch.deleted' => "'y'",'Branch.modified_by' => $this->authUser['id']),
                        array('Branch.id' => $ids)
                    );
                    if ($result) {
                        echo json_encode(array('code' => 0, 'msg' => 'Data Deleted!'));
                    } else {
                        echo json_encode(array('code' => 1, 'msg' => 'Data cannot be deleted'));
                    }
                    break;
            }
        }

        $title_for_layout = 'Branches';
        $page_description = 'Setup And Manage Branches';
        $this->set(compact('title_for_layout','page_description'));
    }

}